<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>java基础 | 流沙 | subtitle</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="">
    <meta name="description" content="String1final class String  implements java.io.Serializable, Comparable&amp;lt;String&amp;gt;, CharSequence 123456789101112131415161718192021222324252627282930313233String s1=&amp;quot;abc&amp;quot;;String s2=&amp;quot;ab">
<meta name="keywords" content="关键字">
<meta property="og:type" content="article">
<meta property="og:title" content="java基础">
<meta property="og:url" content="https://gitwmg.gitee.io/hexoblog/2018/08/14/java基础/index.html">
<meta property="og:site_name" content="流沙">
<meta property="og:description" content="String1final class String  implements java.io.Serializable, Comparable&amp;lt;String&amp;gt;, CharSequence 123456789101112131415161718192021222324252627282930313233String s1=&amp;quot;abc&amp;quot;;String s2=&amp;quot;ab">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-11-01T06:32:56.861Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="java基础">
<meta name="twitter:description" content="String1final class String  implements java.io.Serializable, Comparable&amp;lt;String&amp;gt;, CharSequence 123456789101112131415161718192021222324252627282930313233String s1=&amp;quot;abc&amp;quot;;String s2=&amp;quot;ab">
    
        <link rel="alternate" type="application/atom+xml" title="流沙" href="/hexoblog/atom.xml">
    
    <link rel="shortcut icon" href="/hexoblog/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background:#3F51B5">
      <div class="brand">
        <a href="/hexoblog/" class="avatar waves-effect waves-circle waves-light">
          <img src="/hexoblog/img/avatar.jpeg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">Wang</h5>
          <a href="mailto:919767736@qq.com" title="919767736@qq.com" class="mail">919767736@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/hexoblog/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/hexoblog/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                归档
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/hexoblog/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/hexoblog/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                分类
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="http://www.weibo.com" target="_blank" >
                <i class="icon icon-lg icon-weibo"></i>
                微博
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="http://ftp.wmgblog.pw"  >
                <i class="icon icon-lg icon-link"></i>
                FTP
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">java基础</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
		
		 
		  <a href="/hexoblog/atom.xml" target="_blank" class="header-icon waves-effect waves-circle waves-light rss" id="menuShare">
           <i class="icon icon-lg icon-rss"></i>
        </a>
            
			
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">java基础</h1>
        <h5 class="subtitle">
            
                <time datetime="2018-08-14T01:55:21.000Z" itemprop="datePublished" class="page-time">
  2018-08-14
</time>


            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#String"><span class="post-toc-number">1.</span> <span class="post-toc-text">String</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#String和StringBuffer-StringBuilder"><span class="post-toc-number">2.</span> <span class="post-toc-text">String和StringBuffer StringBuilder</span></a></li></ol>
        </nav>
    </aside>


<article id="post-java基础"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">java基础</h1>
        <div class="post-meta">
            <time class="post-time" title="2018-08-14 09:55:21" datetime="2018-08-14T01:55:21.000Z"  itemprop="datePublished">2018-08-14</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h2 id="String"><a href="#String" class="headerlink" title="String"></a>String</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">final</span> <span class="class"><span class="keyword">class</span> <span class="title">String</span>  <span class="keyword">implements</span> <span class="title">java</span>.<span class="title">io</span>.<span class="title">Serializable</span>, <span class="title">Comparable</span>&lt;<span class="title">String</span>&gt;, <span class="title">CharSequence</span></span></span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">String s1=&quot;abc&quot;;</span><br><span class="line">String s2=&quot;abc&quot;;</span><br><span class="line">System.out.println(s1==s2);</span><br><span class="line">System.out.println(s1.equals(s2));</span><br><span class="line">/*output:</span><br><span class="line">true</span><br><span class="line">true</span><br><span class="line">*/</span><br><span class="line">String s1=new String(&quot;abc&quot;);</span><br><span class="line">String s2=&quot;abc&quot;;</span><br><span class="line">System.out.println(s1==s2); </span><br><span class="line">System.out.println(s1.equals(s2));</span><br><span class="line">/*output:</span><br><span class="line">false</span><br><span class="line">true</span><br><span class="line">*/</span><br><span class="line">String s1=&quot;a&quot;+&quot;b&quot;+&quot;c&quot;;</span><br><span class="line">String s2=&quot;abc&quot;;</span><br><span class="line">System.out.println(s1==s2);</span><br><span class="line">System.out.println(s1.equals(s2));</span><br><span class="line">/*output：</span><br><span class="line">true</span><br><span class="line">true</span><br><span class="line">*/</span><br><span class="line">String s1=&quot;ab&quot;;</span><br><span class="line">String s2=&quot;abc&quot;;</span><br><span class="line">String s3=s1+&quot;c&quot;;</span><br><span class="line">System.out.println(s3==s2);</span><br><span class="line">System.out.println(s3.equals(s2));</span><br><span class="line">/*output：</span><br><span class="line">false</span><br><span class="line">true</span><br><span class="line">*/</span><br></pre></td></tr></table></figure>
<p>常量池存在于方法区中,先在常量池中创建”abc“，并指向s1,而后在创建s2时，由于常量池中已经存在”abc“，只需指向s2就可以，而不需要再创建</p>
<p>new String(“abc”)先在常量池中查找，若没有则创建“abc”，而后通过new在堆内存中创建对象，把“abc”拷贝赋值。String 定义为初始化一个新创建的 String 对象，表示一个与该参数相同的字符序列；换句话说，新创建的字符串是该参数字符串的一个副本。故创建常量池和堆内存中两个对象，两个对象的地址值不一样</p>
<p>java中常量优化机制，编译时s1已经成为“abc”在常量池中查找创建，s2不需要再创建。</p>
<p>对于s3，先创建StringBuilder（或 StringBuffer）对象，通过append连接得到“abc”,再调用toString()转换得到的地址指向s3</p>
<h2 id="String和StringBuffer-StringBuilder"><a href="#String和StringBuffer-StringBuilder" class="headerlink" title="String和StringBuffer StringBuilder"></a>String和StringBuffer StringBuilder</h2><p><code>final class StringBuffer    extends AbstractStringBuilder</code><br>String线程安全,一旦创建了String对象，我们就无法改变它的值。因此，它是线程安全的，可以安全地用于多线程环境中<br>StringBuilder是线程不安全的，运行效率高<br>StringBuffer 线程安全 使用synchronized加了类锁</p>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2018-11-01T06:32:56.861Z" itemprop="dateUpdated">2018-11-01 14:32:56</time>
</span><br>


        
        作者留言区
        
    </div>
    
    <footer>
        <a href="https://gitwmg.gitee.io/hexoblog">
            <img src="/hexoblog/img/avatar.jpeg" alt="Wang">
            Wang
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            

            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://gitwmg.gitee.io/hexoblog/2018/08/14/java基础/&title=《java基础》 — 流沙&pic=https://gitwmg.gitee.io/hexoblog/img/avatar.jpeg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://gitwmg.gitee.io/hexoblog/2018/08/14/java基础/&title=《java基础》 — 流沙&source=描述" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://gitwmg.gitee.io/hexoblog/2018/08/14/java基础/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《java基础》 — 流沙&url=https://gitwmg.gitee.io/hexoblog/2018/08/14/java基础/&via=https://gitwmg.gitee.io/hexoblog" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://gitwmg.gitee.io/hexoblog/2018/08/14/java基础/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/hexoblog/2018/08/16/linuxvpsinit/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">云服务器初始化</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/hexoblog/2018/08/09/ThreadPool/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">线程池</h4>
      </a>
    </div>
  
</nav>



    

















</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/hexoblog/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/hexoblog/img/wechat.jpg" data-alipay="/hexoblog/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="bottom">
        <p><span>Wang &copy; 2017 - 2018</span>
            <span>
                
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://gitwmg.gitee.io/hexoblog/2018/08/14/java基础/&title=《java基础》 — 流沙&pic=https://gitwmg.gitee.io/hexoblog/img/avatar.jpeg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://gitwmg.gitee.io/hexoblog/2018/08/14/java基础/&title=《java基础》 — 流沙&source=描述" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://gitwmg.gitee.io/hexoblog/2018/08/14/java基础/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《java基础》 — 流沙&url=https://gitwmg.gitee.io/hexoblog/2018/08/14/java基础/&via=https://gitwmg.gitee.io/hexoblog" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://gitwmg.gitee.io/hexoblog/2018/08/14/java基础/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/hexoblog/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
